package com.cj.mobile.iabp.mvp.model;

import com.jess.arms.cj.ApiOperator;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.integration.IRepositoryManager;
import com.jess.arms.mvp.BaseModel;
import com.cj.mobile.iabp.mvp.contract.LoginContract;
import com.cj.mobile.iabp.mvp.model.api.service.AccountService;
import com.cj.mobile.iabp.mvp.model.api.service.SystemService;
import com.cj.mobile.iabp.mvp.model.entity.AppUpdate;
import com.jess.arms.cj.CommonResponse;
import com.cj.mobile.iabp.mvp.model.entity.LoginResponse;

import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;

import io.reactivex.Observable;


@ActivityScope
public class LoginModel extends BaseModel implements LoginContract.Model {
    @Inject
    ApiOperator apiOperator;                                                                        // 数据转换

    @Inject
    public LoginModel(IRepositoryManager repositoryManager) {
        super(repositoryManager);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.apiOperator = null;
    }

    @Override
    public Observable<LoginResponse> toLogin(String username, String password) {
        Map<String, Object> params = new HashMap<>();
        params.put("username", username);
        params.put("password", password);

        return apiOperator.chain(params, request -> mRepositoryManager.obtainRetrofitService(AccountService.class).toLogin(request));
    }

    @Override
    public Observable<LoginResponse> quickLogin(String username, String code) {
        Map<String, String> params = new HashMap<>();
        params.put("username", username);
        params.put("code", code);

        return apiOperator.chain(params, request -> mRepositoryManager.obtainRetrofitService(AccountService.class).quickLogin(request));
    }

    @Override
    public Observable<CommonResponse> sendSMS(String mobile) {
        Map<String, String> params = new HashMap<>();
        params.put("phone", mobile);

        return apiOperator.chain(params, request -> mRepositoryManager.obtainRetrofitService(AccountService.class).loginSMS(request));
    }

    @Override
    public Observable<AppUpdate> getVersion(String type) {
        Map<String, Object> params = new HashMap<>();
        params.put("type", type);

        return apiOperator.chain(params, request -> mRepositoryManager.obtainRetrofitService(SystemService.class).getVersion(request));
    }
}